<template>
  <div class="page-search">
    <ZfForm v-bind="searchFormConfig" v-model="formData">
      <template #header>
        <span class="header">高级检索：</span>
      </template>
      <template #footer>
        <div class="handle-btns">
          <el-button icon="refresh" @click="handleRestClick">重置</el-button>
          <el-button type="primary" icon="Search" @click="handleQueryClick"
            >搜索</el-button
          >
        </div>
      </template>
    </ZfForm>
  </div>
</template>

<script lang="ts">
import { defineComponent, ref } from 'vue'
import ZfForm from '@/base-ui/form'

export default defineComponent({
  name: 'user',
  components: {
    ZfForm
  },
  props: {
    searchFormConfig: {
      type: Object,
      reuqired: true
    }
  },
  emits: ['resetBtnClick', 'queryClick'],
  setup(props, { emit }) {
    //双向绑定的属性应该是由配置文件的field来决定
    const formItems = props.searchFormConfig?.formItems ?? []
    const formOriginData: any = {}
    for (const item of formItems) {
      formOriginData[item.field] = ''
    }
    const formData = ref(formOriginData)

    //方式1 采用双向绑定
    // const handleRestClick = () => {
    //   for (const key in formOriginData) {
    //     formData.value[`${key}`] = formOriginData[key]
    //   }
    // }
    //方式2 不采用双向绑定
    const handleRestClick = () => {
      formData.value = formOriginData
      emit('resetBtnClick')
    }

    const handleQueryClick = () => {
      emit('queryClick', formData.value)
    }

    return { formData, handleRestClick, handleQueryClick }
  }
})
</script>

<style scoped>
.header {
  margin-left: 20px;
}
.handle-btns {
  text-align: right;
  padding: 0px 50px 20px 0;
}
</style>
